package de.bazinga.rotw.network.message;

import com.captiveimagination.jgn.message.Message;
import com.captiveimagination.jgn.message.type.CertifiedMessage;
import com.captiveimagination.jgn.message.type.PlayerMessage;
import com.captiveimagination.jgn.message.type.TimestampedMessage;

import de.bazinga.rotw.collision.CollidableObjects;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.network.Server;
import de.bazinga.rotw.objects.GameObject;
import de.bazinga.rotw.person.OpponentList;

public class ItemMessage extends Message implements CertifiedMessage, PlayerMessage, TimestampedMessage, GameMessage {
	public static final int ITEM_HEALTH = 0;
	public static final int ITEM_AMMO = 1;
	public static final int ITEM_ARMOR = 2;
	public static final int ITEM_RIFLE = 3;
	public static final int ITEM_UZI = 4;
	public static final int ITEM_BUTTON = 5;
	public static final int ITEM_HEGRENADE = 6;
	public static final int ITEM_SMOKEGRENADE = 7;
	public static final int ITEM_DOUBLEDAMAGE = 8;
	
	private int item = -1;
	
	private int itemIndex = 0;
	private boolean hide = false;

	public int getItem() {
		return item;
	}

	public void setItem(int item) {
		this.item = item;
	}

	public void setItemIndex(int itemIndex) {
		this.itemIndex = itemIndex;
	}

	public int getItemIndex() {
		return itemIndex;
	}

	public void setHide(boolean hide) {
		this.hide = hide;
	}

	public boolean isHide() {
		return hide;
	}

	
	
	public void handleMessage(RotwGameState game, Message msg, boolean server) {
		if(!(msg instanceof ItemMessage)) return ;

		// Player touched a item
		ItemMessage message = (ItemMessage)msg;
		
		if(server) {
			// Call onCollision, but dont send any messages!
			((GameObject)CollidableObjects.getInstance().getObjects("gameObjects").get(
					message.getItemIndex())).onCollision(
							OpponentList.getInstance().getOpponent(message.getPlayerId()),false);
			
			if( message.isHide() ) {
				((GameObject)CollidableObjects.getInstance().getObjects("gameObjects").get(
						message.getItemIndex())).hide();
				((Server)game.getSyncManager()).getServer()
					.sendToAllExcept((ItemMessage)message, message.getPlayerId());
			}
		}else{
			
			// Player touched a item, so lets hide it
			if( message.isHide() ) {
				((GameObject)CollidableObjects.getInstance().getObjects("gameObjects").get(
						message.getItemIndex())).hide();
			}
		}
	}
}